package com.quickdv.until;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.UUID;

/**
 * Created by Administrator on 2015/6/1.
 */
public class AlertHelper {

    Context context;
    Activity act;

    final public static int SELECT_PICTURE = 1;
    final public static int SELECT_CAMER = 0;

    public AlertHelper(Activity context) {
        this.act = context;
    }

    public AlertDialog BuildCameraDialog() {
        CharSequence[] items = {"相机拍照"

//                , "从相册选择"

        };
        AlertDialog.Builder builder =  new AlertDialog.Builder(act)
                .setTitle("选择图片来源")
                .setItems(items, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        if (which == SELECT_PICTURE) {
                            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                            intent.addCategory(Intent.CATEGORY_OPENABLE);
                            intent.setType("image/*");
                            act.startActivityForResult(Intent.createChooser(intent, "选择图片"), SELECT_PICTURE);
                        } else {
                            String state = Environment.getExternalStorageState();
                            if (state.equals(Environment.MEDIA_MOUNTED)) {
                                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                                // 指定存储照片的路径
                                Uri imageUri = Uri.fromFile(getTempImage());
                                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                                act.startActivityForResult(intent, SELECT_CAMER);
                            } else {
                                Toast.makeText(act, "请确认已经插入SD卡", Toast.LENGTH_LONG).show();
                            }

                        }
                    }
                });
        return builder.create();

    }

    public static File getTempImage() {
        if (android.os.Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED)) {
//            String date =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            File tempFile = new File(Environment.getExternalStorageDirectory(),  "temp.jpg");
            try {
                tempFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return tempFile;
        }
        return null;
    }

    public static File getTempImage2SD() {
        if (android.os.Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED)) {
            File tempFile = new File(Environment.getExternalStorageDirectory(), UUID.randomUUID().toString() +"temp.jpg");
            try {
                tempFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return tempFile;
        }
        return null;
    }

    public ProgressDialog LoadingAlert(){
        ProgressDialog dialog = new ProgressDialog(act);
        dialog.setTitle("加载中");
        dialog.setMessage("努力加载中，请稍后");
        return dialog;
    }
    public ProgressDialog upLoadingAlert(){
        ProgressDialog dialog = new ProgressDialog(act);
        dialog.setTitle("上传中");
        dialog.setMessage("文件上传中，请稍后");
        return dialog;
    }

    public DatePickerDialog dateAlert(DatePickerDialog.OnDateSetListener listener){
        final Calendar c = Calendar.getInstance();
        int mYear = c.get(Calendar.YEAR);
        int mMonth = c.get(Calendar.MONTH);
        int mDay = c.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog dpd = new DatePickerDialog(act,
                listener, mYear, mMonth, mDay);
        return dpd;
    }

}
